package sql.mysql;

import com.sun.jndi.dns.ResourceRecord;
import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
import com.sun.xml.internal.ws.api.pipe.Fiber;
import sun.security.util.Password;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by :Infaraway
 * DATE : 2017/3/4
 * Time : 21:28
 * Funtion :
 */
public class MysqlUtils {


    /**
     * 获取数据库连接
     * @param driver 驱动
     * @param url 地址
     * @param user 用户名
     * @param password 密码
     * @return 返回连接对象
     */
    public static Connection getConnection(String driver, String url, String user, String password) {
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println("mysql connect succeed~");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Connection getConnection() {
        String driver = "com.mysql.jdbc.Driver" ;
        String url = "jdbc:mysql://localhost:3306/hibernate";
        String user = "root";
        String password = "root";
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println("mysql connect succeed~");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     *  查询特定数据库中的所有表
     * @param connection 数据库连接对象
     * @return 数据库表的list
     */
    public static List<String> getTables(Connection connection){
        try {
            List<String> tableList = new ArrayList<>();
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet resultSet = metaData.getTables(null, null, null, new String[]{"TABLE"});
            while (resultSet.next()){
                tableList.add(resultSet.getString(3));
            }
            return tableList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取查询的结果集
     * @param sql sql语句
     * @param parameters 需要查询的参数数组
     * @return 返回结果集
     */
    public static ResultSet select(String sql, Object...parameters) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            System.out.println(sql);
            for (int i = 0; i < parameters.length; i++) {
                preparedStatement.setObject(i+1, parameters[i]);
            }
            //执行给定的 SQL 语句，该语句返回单个 ResultSet 对象
            resultSet = preparedStatement.executeQuery(sql);
            return resultSet;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            connection.close();
        }
        return null;
    }

    /**
     * 修改数据操作
     * @param sql
     * @param parameters
     * @return
     */
    public static boolean update(String sql, Object...parameters) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < parameters.length; i++) {
                preparedStatement.setObject(i+1, parameters[i]);
            }
            System.out.println(sql);
            int num = preparedStatement.executeUpdate();
            if (num != 0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            connection.close();
        }
        return false;
    }

}
